<!DOCTYPE html>
<html>
<head>
    <title>录入住院信息--中软高科-2015</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap-responsive.css"/>
    <link rel="stylesheet" type="text/css" href="../Css/style.css"/>
    <script type="text/javascript" src="../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/jquery.sorted.js"></script>
    <script type="text/javascript" src="../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../Js/ckform.js"></script>
    <script type="text/javascript" src="../Js/common.js"></script>
    <script type="text/javascript" src="../Js/ckeditor/ckeditor.js"></script>
    <script type="text/javascript" src="../Js/webUtil.js"></script>


    <style type="text/css">
        body {
            padding-bottom: 40px;
        }

        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
    <script type="text/javascript">
        $(function () {
            $('#backid').click(function () {
                window.location.href = "index.html";
            });
        });
    </script>
</head>
<body>
<form method="post" class="definewidth m20">
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <td width="10%" class="tableleft">病历号</td>
            <td><input type="text" name="patientId" placeholder="输入病历号回车自动带出挂号人信息"/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">姓名</td>
            <td id="name"></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">证件类型</td>
            <td>身份证</td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">证件号</td>
            <td id="idCard"></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">社保号</td>
            <td id="medical"></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">联系电话</td>
            <td id="phone"></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">是否自费</td>
            <td>
                <input type="radio" name="selfPrice" value="1" disabled>是</input>
                <input type="radio" name="selfPrice" value="0" disabled>否</input>
            </td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">性别</td>
            <td>
                <input type="radio" name="sex" value="0" disabled>男</input>
                <input type="radio" name="sex" value="1" disabled>女</input>
            </td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">年龄</td>
            <td id="age"></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">初复诊</td>
            <td>
                <input type="radio" name="lookDoctor" value="1" disabled>复诊</input>
                <input type="radio" name="lookDoctor" value="0" disabled>初诊</input>
            </td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">所挂科室</td>
            <td id="department">
            </td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">指定医生</td>
            <td id="doctor">

            </td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">备注</td>
            <td><input type="text" name="remark"></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">床位号</td>
            <td><input type="text" name="bedId" value=""/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">缴费押金</td>
            <td><input type="text" name="deposit" value=""/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">病情</td>
            <td><textarea name="illness"></textarea></td>
        </tr>
        <tr>
            <td colspan="2">
                <center>
                    <button class="btn btn-primary" type="button" id="save">保存</button> &nbsp;&nbsp;
                    <button class="btn btn-success" name="backid" id="backid">返回列表
                    </button>
                </center>
            </td>
        </tr>
    </table>
</form>
<span id="ableToSubmit" style="visibility: hidden"></span>
</body>
</html>
<script>
    function fillPatientInfo() {
        let patientId = $("input[name='patientId']").val();
        let flag = false;
        if (!patientId.isPositiveInteger()) {
            alert("病历号格式有误!");
            $("#ableToSubmit").attr("able",false);
        } else {
            $.ajax(
                {
                    type: "GET",
                    url: "/register/" + patientId,
                    success: function (vo) {
                        if (vo.code == 200) {
                            let obj = vo.obj;
                            $("#name").html(obj.name);
                            $("#idCard").html(obj.idCard);
                            $("#medical").html(obj.medical);
                            $("#phone").html(obj.phone);
                            $("input[name='lookDoctor'][value=" + obj.lookDoctor + "]").attr("checked", "checked");
                            $("input[name='sex'][value=" + obj.sex + "]").attr("checked", "checked");
                            $("input[name='selfPrice'][value=" + obj.selfPrice + "]").attr("checked", "checked");
                            $("#age").html(obj.age);
                            $("#department").html(obj.doctor.department.name);
                            $("#doctor").html(obj.doctor.name);
                            $("#ableToSubmit").attr("able",true);
                        } else {
                            alert("未找到对应病历号!");
                            $("#ableToSubmit").attr("able",false);
                        }
                    }
                }
            )
        }
        return flag;
    }

    $(function () {
        $("input[name='patientId']").blur(() => fillPatientInfo())
        $("#save").click(
            function () {
                let bedId = $("input[name='bedId']").val();
                let deposit = $("input[name='deposit']").val();
                let illness = $("textarea[name='illness']").val();

                if (!bedId.isPositiveInteger()) {
                    alert("请输入正确病床号!");
                    return;
                }
                if (!deposit.isNumber()) {
                    alert("请输入正确的款项!");
                    return;
                }
                if (!illness.notEmpty()) {
                    alert("病情不能为空!");
                    return;
                }
                fillPatientInfo();
                if($("#ableToSubmit").attr("able")==false)
                    return;
                $.ajax(
                    {
                        type: "POST",
                        url: "/hospitals/",
                        data: $("form").serialize(),
                        success: function (vo) {
                            if (vo.code == 200) {
                                alert(vo.obj);
                                window.location.href = "index.html"
                            } else
                                alert(vo.msg);
                        }
                    }
                )
            }
        )
    })
</script>